
var time = new Date().valueOf();
threads.start(function () {

    while (true) {

        click("以后再说")


        sleep(1000)
    }
});
console.show();
sleep(1000)
console.setSize(device.width, device.height / 5);
var 免费领花币 = "240541527439405056";

var 领听书会员 = "370895370772901888";
var 新免费领会员 ="413348785930936320"
var intent = new Intent();
intent.setAction("android.settings.APPLICATION_SETTINGS");
自动下载(新免费领会员);
自动下载(领听书会员);

自动下载(免费领花币);

home()
//alert ("卸载完毕");
threads.shutDownAll()
engines.execScriptFile("/sdcard/脚本/音乐抽奖最新.js")
function 自动下载(活动名) {
    重置广告标识符();
    清除音乐()
    打开抽奖页面(活动名);



    //关闭音乐();
}
function 打开抽奖页面(活动名) {

    sleep(1000);
    if (text("同意").findOne(5000)) {
        click("同意")
    }
    sleep(1000);
    app.startActivity({
        action: "android.intent.action.VIEW",
        data: "hwmediacenter://com.android.mediacenter/showH5?pver=121000000&portal=hwmusic&from=com.huawei.musicplatform.campaignh5&needback=0&channelid=1&url=https://campaign-music.hicloud.com/camp/" + 活动名 + "/index.html#/home?operator=8&needback=true&shared=true",
        packageName: "com.android.mediacenter"
    });
    下载app(活动名);
}


function 清除音乐() {
    openAppSetting("com.android.mediacenter");
    while (!click("存储"));
    sleep(1000);
    while (!click("删除数据"));
    sleep(1000);
    while (!click("确定"));
    console.log("已清除数据，等待20s打开音乐");
    sleep(20000);
    app.launchPackage("com.android.mediacenter");

}
function 重置广告标识符() {
    app.startActivity({
        action: "View",
        packageName: "com.huawei.hwid",
        className: "com.huawei.opendevice.open.OAIDSettingActivity"
    });
    log("重置广告")
    sleep(1000);
    while (!click("重置广告标识符"));
    sleep(1000);
    while (!click("重置"));
    sleep(3000);
}
function 下载app(活动名) {
   
    if (textContains("次抽奖机会").findOne(20000)) {
        sleep(5000);
        click(500, 1350)
        sleep(2000)
       click("去登录");
    }
    sleep(1000)
    var 抽奖次数 = textContains("次抽中大奖").findOne();
    抽奖次数 = 抽奖次数.text().split("有")[1].split("次")[0]
    抽奖次数 = 抽奖次数 * 1
    log("抽奖次数为" + 抽奖次数);
    if(抽奖次数<5){
         if(!(抽奖次数==5&&textContains("每天限").findOne(5000))){
    log("等待下载app")

    sleep(15000)
    var 下载体验 = textMatches("下载(体验|安装)").find();//获取下载体验集合
   
    log("下载体验个数为" + 下载体验.length);
    
    if (下载体验.length>0) {//判断
        click("下载体验");
        click("下载体验");
        click("下载安装");
        click("下载安装");
        //获取下载完成数量
        log("等待第一个app下载完成");

        var 打开 = textMatches("(打开|打开抽奖|去薅羊毛)").find();
       
         //判断下载完成数量f
       var time1 = new Date().valueOf()
        while (打开.length < 下载体验.length) {
            var time2 = new Date().valueOf()
            log("等待所有app下载完成");
            sleep(10000);
            if(time2-time1>480000){
                break;
                }
             if(textContains("下载VIP专属歌曲").findOne(100)){
                break
            }
            //是否存在下载失败
            var 下载失败 = text("下载失败").find();
           打开 = textMatches("(打开|打开抽奖|去薅羊毛)").find();
            log("已安装数量" + 打开.length);
            if (下载失败.empty) {
                var num = 打开.length + 下载失败.length;
                if (num == 下载体验.length) {
                    break;
                }
            }
        }
        //遍历点击打开
        if(!textContains("下载VIP专属歌曲").findOne(3000)){
            click("打开");
        click("打开");
        click("去薅羊毛");
        click("去薅羊毛");

        sleep(2000);
        home();
        sleep(10000);
        卸载app1();
        关闭音乐();
        if (抽奖次数*1 + 打开.length*1 < 5) {
            自动下载(活动名)
        }
        }
        
    }else{
    log("下载列表为空或抽奖次数已满");
    自动下载(活动名)
    }
    }
    }
}

function 关闭音乐() {
    log("关闭音乐");

    openAppSetting("com.android.mediacenter");
    //openAppSetting(getPackageName("音乐"));
    while (!click("强行停止"));
    sleep(1000);
    while (!click("强行停止"));
    sleep(1000);

}
function 卸载app() {
    for (let index = 0; index < names.length; index++) {
        if (getPackageName(names[index])) {
            卸载应用(names[index]);

        }
    }
    sleep(20000);
    console.log("卸载完成");
}
function 卸载app1() {
    log("开始卸载");
    var pm = context.getPackageManager()
    var appList = pm.getInstalledApplications(0)

    for (let i = 0; i < appList.size(); i++) {
        var app_ = appList.get(i)
        var appInfo = {
            appName: app_.loadLabel(pm),
            packageName: app_.packageName,
            isSystemApp: app_.isSystemApp(),
            lastUpdateTime: pm.getPackageInfo(app_.packageName, 0).lastUpdateTime
        }
        if (appInfo.lastUpdateTime > time) {
            console.log(appInfo.appName + "卸载中");
            卸载应用(appInfo.appName)
            sleep(15000);
        }
     

    }
    console.log("卸载完成");
}
    function 卸载应用(appname) {
        log(appname + "卸载中");
        app.startActivity(intent);
        sleep(1000);
        setText(appname);
        sleep(1000);
        click(appname, 1);
        sleep(1000);
        while (!click("卸载"));
        sleep(1000);
        while (!click("卸载"));
        sleep(5000);
    }

